package ufr_stgi.assistance_conduite;

import java.io.File;
import java.util.*;

import ufr_stgi.assistance_conduite.modele.TypePanneau;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.*;
import android.widget.*;
import android.widget.ImageView.ScaleType;

/**
 * Construit automatiquement les items de la liste panneaux
 * @author Guillaume MONTAVON & Renaud DAVID (Master 2 RIM)
 */
public class AdapterListePanneaux extends SimpleAdapter {
	
	private List<HashMap<String, String>> listItem;
	private static HashMap<Long, Drawable> listeImageType;
	
	public AdapterListePanneaux(Context context, MonApplication appli, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
		super(context, items, resource, from, to);
		this.listItem = items;
		listeImageType = new HashMap<Long, Drawable>();
		
		for(TypePanneau tp : appli.getBdd().getListeType())
			listeImageType.put(tp.getIdentifiant(), appli.redimensionnerImage(new File(appli.getDossierImage(), tp.getChemin()), 60));
		
	}
	
	/* (non-Javadoc)
	 * @see android.widget.SimpleAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view = super.getView(position, convertView, parent);
		
		long idType = Long.valueOf(listItem.get(position).get("idType"));
		
		//ajoute l'image correspondant au panneau
		if(listeImageType.containsKey(idType)) {
			((ImageView) view.findViewById(R.id.img)).setImageDrawable((Drawable)listeImageType.get(idType));
			((ImageView) view.findViewById(R.id.img)).setScaleType(ScaleType.CENTER);
		}
		
		return view;
		
	}

}
